package com.hsmpay.mobile.service.user.impl;

import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.mobile.dao.user.UserMoneyDao;
import com.hsmpay.mobile.pojo.user.UserMoney;
import com.hsmpay.mobile.service.user.UserMoneyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户虚拟货币 服务接口实现类 userMoneyService
 *
 * @param <Entity>
 * @param <Query>
 * @author 颜铃璋
 *         建立时间：2013-1-24
 */
@Service("userMoneyService")
public class UserMoneyServiceImpl extends BaseServiceSupport<UserMoney, Long> implements UserMoneyService<UserMoney, Long> {
    @Resource(name = "userMoneyDao")
    private UserMoneyDao<UserMoney, Long> userMoneyDao;

    /**
     * 通过userId 查询用户虚拟货币
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public UserMoney searchUserMoneyByUserId(Long userId) throws Exception {
        return userMoneyDao.searchUserMoneyByUserId(userId);
    }

    /**
     * 金额是否足够
     *
     * @param userId
     * @param money
     * @param type
     * @return
     * @throws Exception
     */
    public boolean moneyTheAdequacy(Long userId, Double money, int type) throws Exception {
        UserMoney userMoney = searchUserMoneyByUserId(userId);
        if (null != userMoney) {
            if (1 == type) {
                if (money.doubleValue() <= userMoney.getMoney().doubleValue()) {
                    return true;
                }
            } else if (2 == type) {
                if (money.doubleValue() <= userMoney.getAvailMoney().doubleValue()) {
                    return true;
                }
            }
        }

        return false;
    }

    //---------------------set get start--------------------
    //---------------------set get end----------------------
}
